home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.2 Applications 1996 May
/
SGI IRIX 6.2 Applications 1996 May.iso
/
dist
/
impr_dev.idb
/
usr
/
impressario
/
src
/
libprintui
/
Utilities.c.z
/
Utilities.c
Wrap
C/C++ Source or Header
|
1996-05-06
|
7KB
|
228 lines
/**************************************************************************
*
* Copyright (c) 1993 Silicon Graphics, Inc.
* All Rights Reserved
*
* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
*
* The copyright notice above does not evidence any actual of intended
* publication of such source code, and is an unpublished work by Silicon
* Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
* the property of Silicon Graphics, Inc. Any use, duplication or
* disclosure not specifically authorized by Silicon Graphics is strictly
* prohibited.
*
* RESTRICTED RIGHTS LEGEND:
*
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 52.227-7013,
* and/or in similar or successor clauses in the FAR, DOD or NASA FAR
* Supplement. Unpublished - rights reserved under the Copyright Laws of
* the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
* Shoreline Blvd., Mountain View, CA 94039-7311
**************************************************************************
*
* File: Utilities.c
*
* Description: Utility functions for the Print UI library.
*
**************************************************************************/
#ident "$Revision: 1.1 $"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#include <Xm/Xm.h>
#include <Xm/TextF.h>
#include "PuiI.h"
/**************************************************************************
*
* Function: _PuiExpandFilenames
*
* Description: Performs shell meta character expansion on a list of
* white-space separated filenames. Currently, only tilde expansion
* is performed.
*
* Parameters:
* fnames (I) - string of white-space separated filenames to expand
*
* Return: String of whitespace separated filenames with meta-characters
* expanded. The storage for the string is allocated by the function
* and must be freed by the caller when no longer needed.
*
**************************************************************************/
char* _PuiExpandFilenames(char *fnames)
{
char *srcPtr, *fn, *loc = NULL;
char expFn[PATH_MAX];
char *expFnames = NULL;
/*
* Expand the filenames
*/
srcPtr = fnames;
while ((fn = strtok_r(srcPtr, " \t", &loc)) != NULL) {
srcPtr = NULL;
_PuiExpandTilde(fn, expFn);
if (expFnames) {
expFnames = (char*)XtRealloc(expFnames,
(strlen(expFnames)+strlen(expFn)) * sizeof(char) + 5);
(void)strcat(expFnames, " ");
(void)strcat(expFnames, expFn);
} else {
expFnames = (char*)XtMalloc(strlen(expFn) * sizeof(char) + 5);
(void)strcpy(expFnames, expFn);
}
}
return expFnames;
}
/**************************************************************************
*
* Function: _PuiExpandTilde
*
* Description: If the specified filename begins with a tilde ('~')
* character, this function will expand that character into a
* home directory. The algorithm is as follows:
*
* 1. If the filename does not begin with a tilde, the filename
* is simply copied to the output string.
*
* 2. If the filename begins with a '~' followed immediately by
* '/' or the endof the string, the '~' is expanded into the
* value of the HOME environment variable. If the HOME variable
* is not defined, the tilde is left untouched and the entire
* filename is copied to the output string.
*
* 3. If the filename begins with a '~' followed by a username,
* the tilde is expanded into the specified user's home directory.
*
* Parameters:
* fname (I) - filename to expand
* expFname (O) - expanded filename. Note that storage for this
* variable must be pre-allocated by the caller.
* The storage need be no larger than PATH_MAX as
* defined in limits.h.
*
* Return: none
*
**************************************************************************/
void _PuiExpandTilde(char *fname, char *expFname)
{
register char *p, *c;
char *hptr, lname[256];
struct passwd *pw;
/*
* Sanity check and init the parameters
*/
if (expFname == NULL)
return;
*expFname = '\0';
if (fname == NULL)
return;
/*
* See if the filename begins with a tilde
*/
if (*fname != '~') {
(void)strcpy(expFname, fname);
return;
}
/*
* Get a username after the tilde if any
*/
for (p = lname, c = &fname[1]; *c && *c != '/'; *p++ = *c++)
;
*p = '\0';
/*
* If no username, get HOME env variable value
*/
if (*lname == '\0') {
if ((hptr = getenv("HOME")) == NULL)
(void)strcpy(expFname, "~");
else
(void)strcpy(expFname, hptr);
}
/*
* If username specified after tilde, get user's login dir
*/
else {
if ((pw = getpwnam(lname)) == NULL)
(void)sprintf(expFname, "~%s", lname);
else
(void)strcpy(expFname, pw->pw_dir);
}
/*
* Copy the rest of the filename onto the end of the expanded
* filename
*/
(void)strcat(expFname, c);
}
/**************************************************************************
*
* Function: _PuiCharacterVerifyCB
*
* Description: Checks the character entered at a type-in field against
* a set of illegal characters and disallows what is illegal.
*
* Parameters:
* widget (I) - widget which triggered the callback
* badChars (I) - string of characters to disallow
* callData (I) - text verify callback
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
void _PuiCharacterVerifyCB(Widget widget, XtPointer badChars,
XtPointer callData)
{
register int len;
char *newStr;
XmTextVerifyCallbackStruct *verify = (XmTextVerifyCallbackStruct*)callData;
/*
* Make sure we have something to process. Note that we send back
* a free'able empty string to avoid the Motif bug where the
* the heap can be corrupted when "" is initially set using
* XmTextFieldSetString.
*/
len = verify->text->length;
if (verify->text->ptr == NULL || len == 0) {
verify->text->ptr = XtNewString("");
verify->doit = True;
return;
}
/*
* Check the string against the string of invalid characters
*/
newStr = (char*)XtCalloc(len + 5, sizeof(char));
(void)strncpy(newStr, verify->text->ptr, len);
if (strpbrk(newStr, (char*)badChars) == NULL)
verify->doit = True;
else
verify->doit = False;
XtFree((char*)newStr);
}